/*
 * @Author: your name
 * @Date: 2020-12-31 09:22:31
 * @LastEditTime: 2021-01-04 10:30:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \mock_-device_-data\test.js
 */

/**
 * 封装绘制块方法
 * @param {Array<Array<Number>>} board 画板
 * @param {Number} x x坐标(中心点坐标)
 * @param {Number} y y坐标(中心点坐标)
 * @param {Number} width 块宽度
 * @param {Number} height 块高度
 * @param {Number} type 块数据类型
 */
const draw_block = function(board, x, y, width, height, type) {
  //  一半的宽高
  let width_half  = Math.round(width / 2);
  let height_half = Math.round(height / 2);

  // let top_left_x     = x - random_num(width_half);
  // let top_left_y     = y - random_num(height_half);
  // let top_center_x   = x;
  // let top_center_y   = y - random_num(height_half);
  // let top_right_x    = x + random_num(width_half);
  // let top_right_y    = y - random_num(height_half);

  // let center_left_x  = x - random_num(width_half);
  // let center_left_y  = y;
  // let center_right_x = x + random_num(width_half);
  // let center_right_y = y;

  // let bottom_left_x   = x - random_num(width_half);
  // let bottom_left_y   = y + random_num(height_half);
  // let bottom_center_x = x;
  // let bottom_center_y = y + random_num(height_half);
  // let bottom_right_x  = x + random_num(width_half);
  // let bottom_right_y  = y + random_num(height_half);

  let top_left_x     = 5;
  let top_left_y     = 5;
  let top_center_x   = x;
  let top_center_y   = 0;
  let top_right_x    = x + width_half - 5;
  let top_right_y    = 5;

  let center_left_x  = 0;
  let center_left_y  = y;
  let center_right_x = x + width_half;
  let center_right_y = y;

  let bottom_left_x   = 5;
  let bottom_left_y   = y;
  let bottom_center_x = x;
  let bottom_center_y = y + height_half;
  let bottom_right_x  = x;
  let bottom_right_y  = y;


  let top    = Math.min(top_left_y, top_center_y, top_right_y);
  let bottom = Math.min(bottom_left_y, bottom_center_y, bottom_right_y);
  let left   = Math.min(top_left_x, center_left_x, bottom_left_x);
  let right  = Math.min(top_right_x, center_right_x, bottom_right_x);

  console.log(`top=${top}, bottom=${bottom}, left=${left}, right=${right}`);
  console.log(`top_left_x=${top_left_x}, top_left_y=${top_left_y}, top_center_x=${top_center_x}, top_center_y=${top_center_y}, top_right_x=${top_right_x}, top_right_y=${top_right_y}`);
  console.log(`center_left_x=${center_left_x}, center_left_y=${center_left_y}, center_right_x=${center_right_x}, center_right_y=${center_right_y}`);
  console.log(`bottom_left_x=${bottom_left_x}, bottom_left_y=${bottom_left_y}, bottom_center_x=${bottom_center_x}, bottom_center_y=${bottom_center_y}, bottom_right_x=${bottom_right_x}, bottom_right_y=${bottom_right_y}`);


  //  先copy出此区域的原数据, 然后将此区域填充新的类型数据
  let back_data = JSON.parse(JSON.stringify(board));

  //  画出块左上半部分
  //  1.填充新数据
  for (let i=top_left_y; i<y; i++) {
    for (let j=top_left_x; j<x; j++) {
      board[i][j] = 1;
    }
  }
  //  2.计算左侧三角进行填充, 如果是向内填充则从原数据进行取, 向外填充则填充新的类型数据
  for (let i=top_left_y; i<y; i++) {
    //  宽度差值
    let difference = top_left_x>center_left_x ? (top_left_x-center_left_x) : (center_left_x-top_left_x);
    //  差值 / 行数 = 每行需要累加(减)的数量
    let row_difference = difference / (y-top_left_y);
    //  行计数
    let row_num = i - top_left_y;
    
    //  上宽下窄, 使用旧数据向内填充
    if (center_left_x > top_left_x) {
      for (let j=top_left_x; j<Math.round(top_left_x+row_difference*row_num); j++) {
        board[i][j] = back_data[i][j];
      }
    } else {
      for (let j=top_left_x; j>Math.round(top_left_x-row_difference*row_num); j--) {
        board[i][j] = 1;
      }
    }
  }
  //  3.计算左上三角进行填充, 如果是向内填充则从原数据进行取, 向外填充则填充新的类型数据
  for (let i=top_left_x; i<x; i++) {
    //  宽度差值
    let difference = top_left_y>top_center_y ? (top_left_y-top_center_y) : (top_center_y-top_left_y);
    //  差值 / 行数 = 每行需要累加(减)的数量
    let col_difference = difference / (x-top_left_x);
    //  行计数
    let col_num = i - top_left_x;

    //  左宽右窄, 使用旧数据向内填充
    if (top_left_y < top_center_y) {
      for (let j=top_left_y; j<Math.round(top_left_y+col_difference*col_num); j++) {
        board[j][i] = back_data[j][i];
      }
    } else {
      for (let j=top_left_y; j>Math.round(top_left_y-col_difference*col_num); j--) {
        board[j][i] = 1;
      }
    }
  }

/**
  //  画出块右上半部分
  //  1.填充新数据
  for (let i=top_right_y; i<y; i++) {
    for (let j=x; j<top_right_x; j++) {
      board[i][j] = 1;
    }
  }
  //  2.计算右侧三角进行填充, 如果是向内填充则从原数据进行取, 向外填充则填充新的类型数据
  for (let i=top_right_y; i<y; i++) {
    //  宽度差值
    let difference = top_right_x>center_right_x ? (top_right_x-center_right_x) : (center_right_x-top_right_x);
    //  差值 / 行数 = 每行需要累加(减)的数量
    let row_difference = difference / (y-top_right_y);
    //  行计数
    let row_num = i - top_right_y;
    
    //  上窄下宽, 使用新据向内填充
    if (center_right_x > top_right_x) {
      for (let j=top_right_x; j<Math.round(top_right_x+row_difference*row_num); j++) {
        board[i][j] = 1;
      }
    } else {
      for (let j=top_right_x; j>Math.round(top_right_x-row_difference*row_num); j--) {
        board[i][j] = back_data[i][j];
      }
    }
  }
  // //  3.计算右上三角进行填充, 如果是向内填充则从原数据进行取, 向外填充则填充新的类型数据
  // for (let i=x; i<top_right_x; i++) {
  //   //  宽度差值
  //   let difference = top_right_y>top_center_y ? (top_right_y-top_center_y) : (top_center_y-top_right_y);
  //   //  差值 / 行数 = 每行需要累加(减)的数量
  //   let col_difference = difference / (top_right_x-x);
  //   //  行计数
  //   let col_num = i - x;

  //   //  左宽右窄, 使用新数据向外填充
  //   if (top_center_y < top_right_y) {
  //     for (let j=top_center_y; j<Math.round(top_right_y+col_difference*col_num); j++) {
  //       board[j][i] = 1;
  //     }
  //   } else {
  //     for (let j=top_right_y; j>Math.round(top_right_y-col_difference*col_num); j--) {
  //       board[j][i] = back_data[j][i];
  //     }
  //   }
  // }
**/

}


/**
 * 绘制1/4原型
 * @param {Object} center 中心点坐标
 * @param {Object} left 左边点坐标
 * @param {Object} left_top 左上点坐标
 * @param {Object} top 上点坐标
 */
const draw_1_4_circle = function(center, left, left_top, top) {
  

  //  画出块左上半部分
  //  1.填充新数据
  for (let i=top_left_y; i<y; i++) {
    for (let j=top_left_x; j<x; j++) {
      board[i][j] = 1;
    }
  }

  //  2.计算左侧三角进行填充, 如果是向内填充则从原数据进行取, 向外填充则填充新的类型数据
  for (let i=top_left_y; i<y; i++) {
    //  宽度差值
    let difference = top_left_x>center_left_x ? (top_left_x-center_left_x) : (center_left_x-top_left_x);
    //  差值 / 行数 = 每行需要累加(减)的数量
    let row_difference = difference / (y-top_left_y);
    //  行计数
    let row_num = i - top_left_y;
    
    //  上宽下窄, 使用旧数据向内填充
    if (center_left_x > top_left_x) {
      for (let j=top_left_x; j<Math.round(top_left_x+row_difference*row_num); j++) {
        board[i][j] = back_data[i][j];
      }
    } else {
      for (let j=top_left_x; j>Math.round(top_left_x-row_difference*row_num); j--) {
        board[i][j] = 1;
      }
    }
  }

  //  3.计算左上三角进行填充, 如果是向内填充则从原数据进行取, 向外填充则填充新的类型数据
  for (let i=top_left_x; i<x; i++) {
    //  宽度差值
    let difference = top_left_y>top_center_y ? (top_left_y-top_center_y) : (top_center_y-top_left_y);
    //  差值 / 行数 = 每行需要累加(减)的数量
    let col_difference = difference / (x-top_left_x);
    //  行计数
    let col_num = i - top_left_x;

    //  左宽右窄, 使用旧数据向内填充
    if (top_left_y < top_center_y) {
      for (let j=top_left_y; j<Math.round(top_left_y+col_difference*col_num); j++) {
        board[j][i] = back_data[j][i];
      }
    } else {
      for (let j=top_left_y; j>Math.round(top_left_y-col_difference*col_num); j--) {
        board[j][i] = 1;
      }
    }
  }
}

/**
 * 参照num生成随机数, 例如:num=10, 则随机数范围为 5~15
 * @param {Number} num 随机数参考点
 */
const random_num = function(num) {
  let num_half = num / 2;
  return Math.round(Math.round(Math.random()*num)+num_half);
}


//  1.先准备1张512*512的二维数组, 并填充默认数据(画板)
//  2.绘制块数据(指定 宽/高/中心点/材料(煤炭/矸石)), 在画板上进行随机宽高的绘制
//  3.绘制完成后将二维数组转换成buffer数据

const ROW_LENGTH    = 512;
const COLUMN_LENGTH = 512;

//  画板初始化, 创建二维数组 width * height
let board = new Array(ROW_LENGTH);
for (let i=0; i<ROW_LENGTH; i++) {
  board[i] = new Array(COLUMN_LENGTH);
}

/**
draw_block(board, 20, 20, 20, 20, 1);
for (let i=0; i<20; i++) {
  let str = "|";
  for (let j=0; j<30; j++) {
    str += board[i][j] ? "*" : " ";
  }
  console.log(str);
}
**/

for (let i=0; i<1; i++) {
  let x = Math.round(Math.random() * 20);
  let y = Math.round(Math.random() * 20);
  let width  = Math.round(Math.random() * 10);
  let height = Math.round(Math.random() * 10);

  for (let j=x; j<x+width; j++) {
    for (let k=y; k<y+height; k++) {
      board[k][j] = 1;
    }
  }
}

for (let i=0; i<20; i++) {
  let str = "|";
  for (let j=0; j<30; j++) {
    str += board[i][j] ? "*" : " ";
  }
  console.log(str);
}